import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import path from 'path';

const host = process.env.TAURI_DEV_HOST;

// https://vitejs.dev/config/
export default defineConfig(async () => {
  const config = {
    css: {
      preprocessorOptions: {
        scss: {
          api: 'modern-compiler'
        }
      }
    },

    define: {
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development')
    },

    plugins: [vue()],

    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'), // 配置 @ 指向 src 目录
      },
    },

    // Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build`
    //
    // 1. prevent vite from obscuring rust errors
    clearScreen: false,
    // 2. tauri expects a fixed port, fail if that port is not available
    server: {
      port: 1420,
      strictPort: true,
      host: '0.0.0.0',
      hmr: host
        ? {
          protocol: "ws",
          host,
          port: 1421,
        }
        : undefined,
      watch: {
        // 3. tell vite to ignore watching `src-tauri`
        ignored: ["**/src-tauri/**"],
      },
      proxy: {
        // '/oneapi': {
        //   target: "http://114.132.83.76:1699",
        //   changeOrigin: true,
        //   rewrite: (path) => path.replace(/^\/oneapi/, ''),
        //   ws: true
        // },
        '/oneapi': {
          // target: 'http://localhost:1699',
          target: 'http://114.132.83.76:1699',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/oneapi/, '')
        }
      }
    },

    envPrefix: ['VITE_', 'TAURI_ENV_*'],
    build: {
      // Tauri 在 Windows 上使用 Chromium，在 macOS 和 Linux 上使用 WebKit
      target:
        process.env.TAURI_ENV_PLATFORM == 'windows'
          ? 'chrome105'
          : 'safari13',
      // 在 debug 构建中不使用 minify
      // 在 debug 构建中生成 sourcemap
      sourcemap: !!process.env.TAURI_ENV_DEBUG,
    },
  };
  return config;
});
